package com.yingxin.yxerp.module.activiti.entity.builder;

import com.yingxin.yx.framework.commons.activiti.ApprovalStatus;
import com.yingxin.yx.framework.commons.activiti.dto.ActivitiInstanceDto;
import com.yingxin.yx.framework.commons.activiti.dto.UserTaskDto;
import com.yingxin.yxerp.module.activiti.entity.ActivitiBizInfo;
import com.yingxin.yxerp.module.organization.entity.User;
import org.activiti.engine.history.HistoricTaskInstance;
import org.activiti.engine.task.Comment;
import org.activiti.engine.task.Task;
import org.apache.commons.lang3.StringUtils;

import java.util.List;

public class TaskInstanceDtoBuilder {

    private ActivitiInstanceDto task;

    public TaskInstanceDtoBuilder() {
        this.task = new ActivitiInstanceDto();
    }

    public ActivitiInstanceDto getTask() {
        return task;
    }

    public TaskInstanceDtoBuilder add(ActivitiBizInfo bizInfo) {
        if (bizInfo != null) {
            task.setApplicationTime(bizInfo.getCreateTime());
            task.setBillId(bizInfo.getBizId());
            task.setBillType(bizInfo.getBizType());
            task.setBillNo(bizInfo.getBizNo());
            task.setProcessApprovalStatus(bizInfo.getApprovalStatus());
            task.setProcessInstanceId(bizInfo.getProcessInstanceId());
        }
        return this;
    }

    public TaskInstanceDtoBuilder add(Task task) {
        if (task != null) {
            this.task.setTaskId(task.getId());
            this.task.setTaskName(task.getName());
            this.task.setFormKey(task.getFormKey());
            this.task.setAssigneeId(task.getAssignee());
            this.task.setProcessInstanceId(task.getProcessInstanceId());
        }
        return this;
    }

    public TaskInstanceDtoBuilder add(HistoricTaskInstance task) {
        if (task != null) {
            this.task.setTaskId(task.getId());
            this.task.setTaskName(task.getName());
            this.task.setFormKey(task.getFormKey());
            this.task.setAssigneeId(task.getAssignee());
            this.task.setAssigneeProcessingTime(task.getEndTime());
            this.task.setDeleteReason(task.getDeleteReason());
            this.task.setProcessInstanceId(task.getProcessInstanceId());
        }
        return this;
    }

    public TaskInstanceDtoBuilder add(UserTaskDto task) {
        if (task != null) {
            this.task.setTaskId(task.getTaskId());
            this.task.setTaskName(task.getTaskName());
            this.task.setFormKey(task.getFormKey());
            this.task.setAssigneeId(task.getAssigneeId());
            this.task.setAssigneeProcessingTime(task.getEndTime());
            this.task.setDeleteReason(task.getDeleteReason());
            this.task.setProcessInstanceId(task.getProcessInstanceId());

            this.task.setApplicationTime(task.getBizCreateTime());
            this.task.setBillId(task.getBizId());
            this.task.setBillType(task.getBizType());
            this.task.setBillNo(task.getBizNo());
            this.task.setProcessApprovalStatus(task.getProcessApprovalStatus());
            this.task.setCurrentAssigneeInfo( task.getCurrentAssigneeInfo() );
        }
        return this;
    }

    public TaskInstanceDtoBuilder add(User startUser) {
        if (startUser != null) {
            task.setApplicantId(startUser.getId());
            task.setApplicantName(startUser.getStaffName());
        }
        return this;
    }

    public TaskInstanceDtoBuilder addComments(List<Comment> comments) {
        if (comments != null) {
            if (comments.size() > 0) {
                task.setAssigneeComment(comments.get(0).getFullMessage());
            }
        }
        return this;
    }

    public TaskInstanceDtoBuilder addHitTasks(List<ActivitiInstanceDto> hitTasks) {
        this.task.setHitTasks(hitTasks);
        return this;
    }

    public TaskInstanceDtoBuilder addAssigneeName(User assigneeUser) {
        if (assigneeUser != null) {
            this.task.setAssigneeName(assigneeUser.getStaffName());
        }
        return this;
    }


    public TaskInstanceDtoBuilder taskApprovalStatus() {
        if (this.task.getAssigneeProcessingTime() == null) {
            this.task.setTaskApprovalStatus(ApprovalStatus.PENDING_APPROVAL.getStatus());
        } else {
            if (StringUtils.isEmpty(this.task.getDeleteReason())) {
                this.task.setTaskApprovalStatus(ApprovalStatus.APPROVED.getStatus());
            } else {
                this.task.setTaskApprovalStatus(this.task.getProcessApprovalStatus());
            }
        }
        return this;
    }


}
